<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script>
  /**封装blob对象*/
  function dataURLtoBlob(base64Str, mimeTypeStr) {
    var bstr = atob(base64Str), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mimeTypeStr });
  }
		/**创建一个a标签，并做下载点击事件*/
  function downloadFile(blobUrl, fileName){
    console.log('blobUrl',blobUrl);
    var a = document.createElement("a")
    a.setAttribute("href", blobUrl)
    a.setAttribute("download", fileName)
    a.setAttribute("target", "_blank")
    a.click()    
    // let clickEvent = document.createEvent("MouseEvents");
    // clickEvent.initEvent("click", true, true);  
    // a.dispatchEvent(clickEvent);
  }
		/**下载文件的方法*/
  function downloadFileByBase64(base64Str, mimeTypeStr, fileName){
    var myBlob = dataURLtoBlob(base64Str, mimeTypeStr)
    var blobUrl = URL.createObjectURL(myBlob)
    downloadFile(blobUrl,fileName)
  }
/*************************** 测试代码 start ************************/
  function download(){
    const obj = {
      fileStr:'iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAACXBIWXMAAAsSAAALEgHS3X78AAAEk0lEQVRYhbVXS09bRxQ+N3bNRdjEai3UYiGMYdFFUKzykKoKSpWN1QU1YtVVCavu4vML4vQPDFGURRYosIGtQxeVF8hG2cGiICMUVUh1awlSQaldVLBsB1fncsaM773Dq80nWXPndeabOd85MzYajQbcBogYBIAYAGwKIUq3MsK442i5BhBxDgD+AoAslVy/NW58Eog4AwAvHR0AD4UQC47Wa+A2J5HgcgsA+rgkzDhGvkcSQeWbtLDpGHFDeHXDWXi06wiLL81dVH4JAPcBoAAAd7k9p8yd4XnUn75KuK6aYAI5XkhiSwgR437y/XdK3yshREI3DwAmLiOic8ecYmiNy/uImKQPIQTt9AW3vyAC/D3jNo/taaEjE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Pn18PCw1N3dHYJzYZ4eHx//Y5pmSDFRcDenF+ZlmFNyx0uZQyqVyjM5x+PxtAeDQZXA2v9Kgq9z+QJf4/KraDT6A905jgnngtQ9hCxo35j/AfKZSLDuhqtsvQ8SNwMA/AsjMqbmUsz4jAAAAABJRU5ErkJggg==',
      fileType:'image/png',
      fileName: '图片',
    }
    const {fileName,fileStr,fileType} = obj
    downloadFileByBase64(fileStr, fileType, fileName);
  }
 
		/*************************** 测试代码 end ************************/
</script>
<body>
  <div onclick="download()">点我下载</div>
</body>
</html>